package com.xgkj.diyiketang.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.xgkj.diyiketang.R;
import com.xgkj.diyiketang.data.EatShowUserInfo;
import com.xgkj.diyiketang.http.engine.ShowToastTaskEventListener;
import com.xgkj.diyiketang.http.engine.TaskEventListener;
import com.xgkj.diyiketang.task.CheckPwdTask;
import com.xgkj.diyiketang.util.ImageCache;
import com.xgkj.diyiketang.util.ViewUtil;
import com.xgkj.diyiketang.weidget.ClearableEditText;
import com.xgkj.diyiketang.weidget.RoundImageView;
import com.xgkj.diyiketang.yizhibo.Constants;
import com.xgkj.diyiketang.yizhibo.PlayerActivity;
import com.xgkj.diyiketang.yizhibo.PlayerNewActivity;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;

public class ZhiboPasswordActivity extends BaseActivity implements View.OnClickListener{

    private ImageView closeBtn;
    private TextView title;
    private TextView passwordRoom;
    private RoundImageView headImg;
    private TextView nickname;
    private ClearableEditText passwordEdit;
    private Context mContext;
    private String headUrl;
    private String nickNameS;
    private String vid;
    private String pwd;
    private int checkPwdTaskId = -1;
    private String liveState;
    private String zhibo_play;
    //    private ArrayList<EatShowUserInfo> zhiboList;
//    private int currentPosition ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_zhibo_password);
        mContext = this;
        initView();
    }

    private void initView() {
        Intent intent = getIntent();
        if (intent != null){
            headUrl = intent.getStringExtra("head_url");
            nickNameS = intent.getStringExtra("nickname");
            liveState = intent.getExtras().getString(Constants.LIVE_STATE);
            zhibo_play = intent.getExtras().getString(Constants.ZHIBO_PLAY);
            vid = intent.getStringExtra(Constants.EXTRA_KEY_VIDEO_ID);
//            zhiboList = (ArrayList<EatShowUserInfo>) getIntent().getSerializableExtra("ZHIBO_LIST");
//            currentPosition = getIntent().getIntExtra("CURRENT_POSITION",0);
        }
        closeBtn = (ImageView) findViewById(R.id.close_btn);
        closeBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        title = (TextView) findViewById(R.id.title);
        passwordRoom = (TextView) findViewById(R.id.password_room);
        headImg = (RoundImageView) findViewById(R.id.head_img);
        if (headUrl!= null && headUrl.length()>0) {
            ImageCache.getInstance(mContext).loadBitmapToView(headImg, headUrl, ViewUtil.getHeadImageSize(mContext, 70));
        }
        nickname = (TextView) findViewById(R.id.nickname);
        if (nickNameS != null){
            try {
                nickNameS = URLDecoder.decode(nickNameS,"utf-8");
                nickname.setText(nickNameS);
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
        passwordEdit = (ClearableEditText) findViewById(R.id.password_edit);
        findViewById(R.id.sure_btn).setOnClickListener(this);

    }

    private void checkPwd(){
        CheckPwdTask checkPwdTask = new CheckPwdTask(vid,pwd);
        checkPwdTask.addListener(mListener);
        checkPwdTaskId = getEatShowApp().getTaskManager().addTask(checkPwdTask);
        ViewUtil.showProgressDialog(mContext,getString(R.string.get_pageinfo_loading),checkPwdTaskId);
    }

    private TaskEventListener mListener = new ShowToastTaskEventListener(this){
        @Override
        public void handleTaskCompleted(int taskId, int result, Object data) {
            super.handleTaskCompleted(taskId, result, data);
            if (taskId==checkPwdTaskId){
                ViewUtil.dismissDialog();
                Intent intent = new Intent(mContext, PlayerNewActivity.class);
                intent.putExtra(Constants.EXTRA_KEY_VIDEO_ID, vid);
                intent.putExtra(Constants.ZHIBO_PLAY, zhibo_play);
                intent.putExtra("type", "pwd");
                intent.putExtra("liveState",liveState);
//                intent.putExtra("ZHIBO_LIST", zhiboList);
//                intent.putExtra("CURRENT_POSITION", currentPosition);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                mContext.startActivity(intent);
                finish();
            }
        }

        @Override
        public void handleTaskError(int taskId, int errorCode, String errorMsg) {
            super.handleTaskError(taskId, errorCode, errorMsg);
            ViewUtil.dismissDialog();
        }
    };

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.sure_btn:
                if (checkInput()){
                    checkPwd();
                }
                break;
        }
    }

    private boolean checkInput() {
        pwd = passwordEdit.getText().toString();
        if (ViewUtil.isNull(pwd)){
            Toast.makeText(mContext,R.string.no_psw,Toast.LENGTH_SHORT).show();
            return false;
        }
        return true;
    }
}
